<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : functions.xsl
    Created on : 9. květen 2011, 15:46
    Author     : Martin Řehánek, rehan at mzk.cz
    Description:
        Functions used in other templates
-->

<xsl:stylesheet version="1.0"
                xmlns:marc="http://www.loc.gov/MARC21/slim"
                xmlns:europeana="http://www.europeana.eu/schemas/ese/"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="marc">
                    
    <xsl:variable name="imageserverUrl">http://imageserver.mzk.cz</xsl:variable>
    <xsl:variable name="adamUrl">http://aleph.mzk.cz/adam</xsl:variable>
    <xsl:variable name="mapyUrl">http://mapy.mzk.cz</xsl:variable>
    <xsl:variable name="k4Url">http://kramerius4.mzk.cz</xsl:variable>
    
    

    <xsl:template name="trimTo100chars">
        <xsl:param name="text"/>
        <xsl:variable name="textNormalized" select="normalize-space($text)"/>
        <xsl:variable name="maxLength">100</xsl:variable>
        <xsl:choose>
            <xsl:when test="string-length($textNormalized) &gt; $maxLength">
                <xsl:value-of select="substring($textNormalized,1,97)"/>...
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>...
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="removeFirstCharIfEquals">
        <xsl:param name="text"/>
        <xsl:param name="char"/>
        <xsl:choose>
            <xsl:when test="starts-with($text, $char)">
                <xsl:variable name="newSize" select="string-length($text) - number('1')"/>
                <xsl:value-of select="substring($text,2,$newSize)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="removeLastCharIfEquals">
        <xsl:param name="text"/>
        <xsl:param name="char"/>
        <xsl:choose>
            <xsl:when test="ends-with($text, $char)">
                <xsl:variable name="newSize" select="string-length($text) - number('1')"/>
                <xsl:value-of select="substring($text,1,$newSize)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="removeEndComma">
        <xsl:param name="text"/>
        <xsl:call-template name="removeLastCharIfEquals">
            <xsl:with-param name="text" select="$text"/>
            <xsl:with-param name="char">,</xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="removeOpeningSquareBracket">
        <xsl:param name="text"/>
        <xsl:call-template name="removeFirstCharIfEquals">
            <xsl:with-param name="text" select="$text"/>
            <xsl:with-param name="char">[</xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="removeClosingSquareBracket">
        <xsl:param name="text"/>
        <xsl:call-template name="removeLastCharIfEquals">
            <xsl:with-param name="text" select="$text"/>
            <xsl:with-param name="char">]</xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="removeSquareBrackets">
        <xsl:param name="text"/>
        <xsl:variable name="withoutOpeningBracket">
            <xsl:call-template name="removeOpeningSquareBracket">
                <xsl:with-param name="text" select="$text"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="removeClosingSquareBracket">
            <xsl:with-param name="text" select="$withoutOpeningBracket"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="addEndCharIfMissing">
        <xsl:param name="text"/>
        <xsl:param name="char"/>
        <xsl:choose>
            <xsl:when test="ends-with($text, $char)">
                <xsl:value-of select="$text"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
                <xsl:value-of select="$char"/>
            </xsl:otherwise>
        </xsl:choose>    
    </xsl:template>
    
    <xsl:template name="addClosingBracketIfMissing">
        <xsl:param name="text"/>
        <xsl:call-template name="addEndCharIfMissing">
            <xsl:with-param name="text" select="$text"/>
            <xsl:with-param name="char">]</xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="addOpeningSquareBracketIfMissing">
        <xsl:param name="text"/>
        <xsl:if test="not(starts-with($text, '['))">
            <xsl:if test="ends-with($text, ']')">[</xsl:if>
        </xsl:if>
        <xsl:value-of select="$text"/>
    </xsl:template>

    <xsl:template name="thumbnail">
        <xsl:variable name="sub856u" select="marc:datafield[@tag='856']/marc:subfield[@code='u']"/>
        <xsl:choose>
            <xsl:when test="starts-with($sub856u, $imageserverUrl)">
                <xsl:value-of select="$sub856u"/>/large.jpg
            </xsl:when>
<!--            <xsl:when test="starts-with($sub856u, $mapyUrl)">
                <xsl:value-of select="$sub856u"/>
            </xsl:when>-->
            <xsl:when test="starts-with($sub856u, $adamUrl)">
                <xsl:value-of select="$sub856u"/>
            </xsl:when>
            
            <xsl:otherwise>
                <!--prazdna 856u, co ted? Pokud se to stane, jde o chybu.
                   Zaznamy bez 856u se nemaji transformovat do ese
               -->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="imgServer_big_picture">
        <xsl:variable name="sub856u" select="marc:datafield[@tag='856']/marc:subfield[@code='u']"/>
        <xsl:choose>
<!--            TODO: pouzit jiny nahled-->
            <xsl:when test="starts-with($sub856u, $imageserverUrl)">
                <xsl:value-of select="$sub856u"/>/large.jpg
            </xsl:when>
<!--            <xsl:when test="starts-with($sub856u, $mapyUrl)">
                <xsl:value-of select="$sub856u"/>
            </xsl:when>-->
            <xsl:when test="starts-with($sub856u, $adamUrl)">
                <xsl:value-of select="$sub856u"/>
            </xsl:when>
            
            <xsl:otherwise>
                <!--prazdna 856u, co ted? Pokud se to stane, jde o chybu.
                   Zaznamy bez 856u se nemaji transformovat do ese
               -->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="k4_picture_from_handle">
        <xsl:variable name="sub856u" select="marc:datafield[@tag='856']/marc:subfield[@code='u']"/>
        <xsl:variable name="uuid" select="substring-after(substring-after($sub856u,$k4Url),'/search/handle/uuid:')"/>
        <xsl:value-of select="$k4Url"/>/search/img?uuid=<xsl:value-of select="$uuid"/>&amp;stream=IMG_PREVIEW&amp;action=GETRAW
    </xsl:template>
    
    <xsl:template name="alephLink">
        <xsl:variable name="link">
            http://aleph.mzk.cz/F?func=direct&amp;doc_number=<xsl:value-of select="marc:controlfield[@tag=001]"/>&amp;local_base=MZK03&amp;format=999
        </xsl:variable>
        <xsl:value-of select="normalize-space($link)"/>
    </xsl:template>
    
    <xsl:template name="k4_link_from_856">
        <xsl:variable name="sub856u" select="marc:datafield[@tag='856']/marc:subfield[@code='u']"/>
        <xsl:value-of select="$sub856u"/>
    </xsl:template>
    
</xsl:stylesheet>
